<template>
  <div class="detail-page">
    <header class="header">
      <h1>{{ article.stem }}</h1>
      <p>
        {{ article.createdAt }} | {{ article.views }} 浏览量 |
        {{ article.likeCount }} 点赞数
      </p>
      <p>
        <img :src="article.avatar" alt="" />
        <span>{{ article.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="article.content"></main>
    <div class="opt">
      <van-icon @click="changeState(1)" :class="{active:article.likeFlag}" name="like-o" />
      <van-icon @click="changeState(2)" :class="{active:article.collectFlag}" name="star-o" />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import type { QuestionItem } from '@/types/data'
import { getAricleInfo, likeOrCollect } from '@/api/article'
import { useRoute } from 'vue-router'
const route = useRoute()
const article = ref<Partial<QuestionItem>>({})

onMounted(() => {
  getDetail()
})
const getDetail = async () => {
  const { data } = await getAricleInfo({ id: route.params.id as string })
  article.value = data
}
// 切换状态
const changeState = async (type: 1 | 2) => {
  await likeOrCollect({ id: article.value.id as string, optType: type })
  if (type === 1) {
    article.value.likeFlag = article.value.likeFlag === 1 ? 0 : 1
    if (article.value.likeFlag) {
      article.value.likeCount && article.value.likeCount++
    } else {
      article.value.likeCount && article.value.likeCount--
    }
  } else {
    article.value.collectFlag = article.value.collectFlag === 1 ? 0 : 1
  }
}
</script>
<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;

  .header {
    h1 {
      font-size: 24px;
    }

    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }

    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }

  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;

    >.van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;

      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>